package com.xiaomili.musicservice;

import android.app.Service;
import android.content.Intent;
import android.media.MediaPlayer;
import android.os.Binder;
import android.os.Environment;
import android.os.IBinder;
import android.util.Log;

import java.io.File;
import java.io.IOException;
import java.security.PublicKey;

public class MyMusicService extends Service {
    private static final String TAG = "MyMusicService";
    private MediaPlayer mp;
    private String fileName;
    private boolean isPrepared = true;

    public MyMusicService() {

    }

    @Override
    public void onCreate() {
        fileName = Environment.getExternalStorageDirectory().getAbsolutePath() + "/wang.mp3";
        Log.i(TAG, "fileName:  " + fileName);
        super.onCreate();
        //实例化
        mp = new MediaPlayer();
        try {
            mp.reset();
            mp.setDataSource(fileName);
            mp.prepareAsync();
        } catch (IOException e) {
            e.printStackTrace();
        }
        mp.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
            @Override
            public void onCompletion(MediaPlayer mp) {
                mp.start();
            }
        });
    }

    @Override
    public IBinder onBind(Intent intent) {
        return new MyBinder();
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        int order = intent.getIntExtra(MainActivity.ORDER_KEY, MainActivity.ORDER_UNKNOW);
        final int progress=intent.getIntExtra(MainActivity.SET_PROGRESS,0);
        switch (order) {
            case MainActivity.ORDER_PLAY:
                if (!isPrepared) {
                    mp.prepareAsync();
                    mp.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
                        @Override
                        public void onPrepared(MediaPlayer mp) {
                            mp.seekTo(0);
                            mp.start();
                            isPrepared = true;
                            Log.i(TAG, "start current-->" + mp.getCurrentPosition());
                            Log.i(TAG, "start duration-->" + mp.getDuration());
                        }
                    });
                } else {
                    mp.start();
                }
                mp.setOnSeekCompleteListener(new MediaPlayer.OnSeekCompleteListener() {
                    @Override
                    public void onSeekComplete(MediaPlayer mp) {
                        mp.seekTo(progress);
                        mp.start();
                    }
                });
                break;
            case MainActivity.ORDER_PAUSE:
                mp.pause();
                break;
            case MainActivity.ORDER_STOP:
                mp.stop();
                isPrepared = false;
                Log.i(TAG, "stop current-->" + mp.getCurrentPosition());
                Log.i(TAG, "stop duration-->" + mp.getDuration());
                break;
            case MainActivity.ORDER_UNKNOW:
                break;
        }
        return super.onStartCommand(intent, flags, startId);
    }

    @Override
    public boolean onUnbind(Intent intent) {
        return super.onUnbind(intent);
    }

    public class MyBinder extends Binder {
        public int[] getMusicTime() {
            int[] timer = new int[2];
            timer[0] = mp.getDuration();
            if (!isPrepared) {
                timer[1] = 0;
            } else {
                timer[1] = mp.getCurrentPosition();
            }
            return timer;
        }

        public boolean isMusicPlaying() {
            if (mp != null) {
                return mp.isPlaying();
            }
            return false;

        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        if (mp != null) {
            mp.release();
            mp = null;
        }
    }
}
